<template>
    <div class="login_container">
        <!--        登录盒子-->
        <div class="login_box">
            <!--            头像-->
            <div class="avatar_box">
                <img src="../assets/logo.png" alt="">
            </div>
            <!--            登录表单-->
            <el-form :model="loginForm" :rules="loginFormRules" ref="loginForm" class="login_form">
                <el-form-item prop="username">
                    <el-input prefix-icon="el-icon-user-solid" v-model="loginForm.username"></el-input>
                </el-form-item>
                <el-form-item prop="password">
                    <el-input prefix-icon="el-icon-lock" type="password" v-model="loginForm.password"></el-input>
                </el-form-item>
                <el-form-item class="login_btns">
                    <el-button type="primary" @click="login">登录</el-button>
                    <el-button type="info" @click="loginResetForm">重置</el-button>
                </el-form-item>
            </el-form>
        </div>

    </div>
</template>

<script>


import loginAPI from "../http/loginAPI";

export default {
    created() {
        console.log(this.$router);
        console.log(this.$route);
    },
    name: "Login",
    data() {
        return {
            //登录表单数据
            loginForm: {
                username: "admin",
                password: "123456"
            },
            //登录表单数据验证规则
            loginFormRules: {
                username: [
                    //required必填  trigger(触发规则) 失去焦点
                    { required: true, message: '请输入用户名称', trigger: 'blur' },
                    { min: 5, max: 8, message: '长度在 5 到 8 个字符', trigger: 'blur' }
                ],
                password: [
                    { required: true, message: '请输入密码', trigger: 'blur' },
                    { min: 5, max: 8, message: '长度在 5 到 8 个字符', trigger: 'blur' }
                ],
            }
        }
    },
    methods:{
        //重置表单方法
        loginResetForm() {
            //通过$refs获取当前页面所有组件,然后找到loginForm组件,并且执行resetFields(elform组件自带的重置)方法
            this.$refs.loginForm.resetFields();
        },
        //点击登录按钮
        login(){
            console.log(this)
            console.log(this.$router)
            this.$refs.loginForm.validate(async (valid)=>{
                //如果valid为true 则表示整个表单内的校验都通过了
                if(!valid) return;
                //通过校验

                //发送请求到后端进行真正的登录
                // let {data:res} = await axios.post("login", this.loginForm);
                // let {data:res} = await this.$http.post("login", this.loginForm);
                let {data:res} = await loginAPI.login(this.loginForm);
                if(res.meta.status !== 200) return this.$message.error(res.meta.msg);
                //登录成功
                this.$message.success(res.meta.msg);
                //把token值存储到本地存储
                window.sessionStorage.setItem("token", res.data.token)
                //成功后=>跳转到首页
                await this.$router.push("/home")
            })
        }
    }
}
</script>

<style scoped lang="scss">
.login_container{
    position: relative;
    background-color: #2b4b6b;
    height: 100%;
    .login_box{
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
        width: 450px;
        height: 300px;
        background-color: #ffffff;
        border-radius: 3px;
        /*box-shadow: 上下偏移 左右偏移 扩散范围 颜色;*/
        box-shadow: 0 0 15px #dddddd;
        .avatar_box{
            position: absolute;
            left: 50%;
            transform: translate(-50%, -50%);
            border: 1px solid #efefef;
            border-radius: 50%;
            padding: 10px;
            width: 150px;
            height: 150px;
            background-color: #ffffff;
            box-shadow: 0 0 15px #dddddd;
            img{
                width: 100%;
                height: 100%;
                border-radius: 50%;
                background-color: #efefef;
            }
        }
        .login_form{
            position: absolute;
            bottom: 0;
            padding: 0 20px;
            width: 100%;
            .login_btns{
                display: flex;
                justify-content: flex-end;
            }
        }
    }
}
</style>